package com.quanshu.exchange.ui.gui.message.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.chat.ChatConstant;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;
import com.quanshu.exchange.ui.gui.message.chat.ChatActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jye on 2017/12/25.
 */

public class SearchGroupActivity extends BaseActivity {

    @BindView(R.id.search_bar_editText)
    EditText searchBarEditText;
    @BindView(R.id.search_bar_delete)
    ImageButton searchBarDelete;
    @BindView(R.id.search_bar_search_btn)
    TextView searchBarSearchBtn;
    @BindView(R.id.search_recycler_view)
    RecyclerView recyclerView;

    private String keyWords;
    private SingleAdapter<GroupEntity> adapter;
    private List<GroupEntity> datas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_group);
        ButterKnife.bind(this);
        initView();
        initListener();
    }


    private void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleAdapter(new DataDisplayer<GroupEntity>(this, R.layout.item_group_layout) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper wrapper, GroupEntity item, int position) {
                holder.setRoundImageUrl(R.id.group_avatar, item.getGroup_photo(), R.drawable.default_group_avatar);
                holder.setText(R.id.group_name, item.getGroup_name());
                holder.setText(R.id.group_desc, item.getGroup_introduce());
                holder.setVisibility(R.id.group_is_added, View.GONE);
                holder.setVisibility(R.id.group_is_added, View.VISIBLE);
                if (item.getIs_have() == 1) {
                    holder.setText(R.id.group_is_added, "已加入");
                    holder.getView(R.id.group_is_added).setEnabled(false);
                } else {
                    holder.setText(R.id.group_is_added, "申请加入");
                    holder.getView(R.id.group_is_added).setEnabled(true);
                }
                holder.setOnClickListener(R.id.group_is_added, view -> doJoinGroup(item));
                holder.setOnClickListener(view -> goGroupDetailActivity(item));
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(searchBarDelete, aVoid -> searchBarEditText.setText(""));
        RxViewHelper.getInstance().clicks(searchBarSearchBtn, aVoid -> finish());
        RxViewHelper.getInstance().textChangeEvents(searchBarEditText, textViewTextChangeEvent -> {
            keyWords = textViewTextChangeEvent.text().toString();
            initData();
        });

    }

    private void initData() {
        if (TextUtils.isEmpty(keyWords)) {
            return;
        }
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .getSearchGroups(AppUtils.getSignParams(currentUser.getMemberid(), keyWords), currentUser.getMemberid(), keyWords)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<GroupEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<GroupEntity>> response) {
                        if (response.getInfo() != null) {
                            datas = response.getInfo();
                            adapter.setData(datas);
                        }
                    }
                });
    }

    private void doJoinGroup(GroupEntity groupEntity) {
        if (groupEntity.getIs_verify() == 1) {
            Intent intent = new Intent(this, SendGroupApplyActivity.class);
            intent.putExtra("GroupEntity", groupEntity);
            startActivity(intent);
        } else {
            doSyncService(groupEntity);
        }
    }

    private void goGroupDetailActivity(GroupEntity groupEntity) {
        Intent intent = new Intent(this, GroupChatDetailActivity.class);
        intent.putExtra("GroupEntity", groupEntity);
        startActivity(intent);
    }

    private void doSyncService(GroupEntity groupEntity) {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .joinGroup(AppUtils.getSignParams(currentUser.getMemberid(), groupEntity.getHx_groupid()), currentUser.getMemberid(), groupEntity.getHx_groupid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        Intent intent = new Intent(SearchGroupActivity.this, ChatActivity.class);
                        intent.putExtra("CHAT_TYPE", ChatConstant.CHAT_TYPE_GROUP);
                        intent.putExtra("conversationID", groupEntity.getHx_groupid());
                        startActivity(intent);
                    }
                });
    }

}
